#pragma once
#include "DXContext.h"
#include "primitives.h"
#include "ErrorCodes.h"

namespace DXBase
{
	namespace D2D
	{
		[Windows::Foundation::Metadata::WebHostHidden]
		public ref class Bitmap sealed : IContextBound //IRenderTarget
		{
			ContextWatcher watcher;
			std::function<void(Bitmap^)> loadDX;

			Windows::Foundation::Size size;
			USize pixelSize;

		internal:
			Microsoft::WRL::ComPtr<ID2D1Bitmap> pBitmap;
			//NativeRenderTarget^ ntarget;

			virtual void LoadDXData() = IContextBound::LoadDXData;
			virtual void ClearDXData() = IContextBound::ClearDXData 
			{
				//ntarget = nullptr;
				pBitmap = nullptr; 
				size = Windows::Foundation::Size();
			}
			virtual property DXContext^ Context
			{
				DXContext^ get() = IContextBound::Context::get { return watcher.Context; } 
				void set(DXContext^ value) = IContextBound::Context::set 
				{
					//if (value && Context && value != Context && Context->Target == this)
					//	throw ExHelper::CreateException(ErrorCodes::TargetIsInUse);
					watcher.Context = value; 
				}
			}

			//virtual property Windows::Graphics::Display::DisplayOrientations Orientation { Windows::Graphics::Display::DisplayOrientations get() = IRenderTarget::Orientation::get { return Windows::Graphics::Display::DisplayOrientations::Landscape; } }
			//virtual void BeginRender() = IRenderTarget::BeginRender {};
			//virtual void EndRender() = IRenderTarget::EndRender {};
			//virtual property float Dpi { float get() = IRenderTarget::Dpi::get { return 96.0f; } }
			//virtual NativeRenderTarget^ GetNativeTarget() = IRenderTarget::GetNativeTarget;
			//virtual D3D::Texture2D^ Snapshot() = IRenderTarget::Snapshot;

		public:
			Bitmap();

			// TODO other ID2D1Bitmap methods

			property Windows::Foundation::Size Size { Windows::Foundation::Size get() { return size; } }
			property USize PixelSize { USize get() { return pixelSize; } }

			void Create(UINT32 width, UINT32 height);
			void CreateWic(Platform::String^ filename);
		};
	}
}